<?php

/**
 * 
 * @author yuxun
 *
 */
class InsidePlayerService extends Service{

    public static function indexHeader(){
        $header = array(
            'id'           => array('text' => 'ID', 'width' => '90px'),
            'special_id'   => array('text' => '专服', 'width' => '160px'),
        	'server_id'    => array('text' => '服务器', 'width' => '100px'),
        	'role_name'    => array('text' => '角色名', 'width' => '160px'),
        	'role_id'      => array('text' => '角色ID', 'width' => '160px'),
        	'account'      => array('text' => '账号', 'width' => '160px'),
        	'real_name'    => array('text' => '真实姓名', 'width' => '100px'),
        	'position'     => array('text' => '职位', 'width' => '100px'),
            'is_qx'        => array('text' => '是否趣炫', 'width' => '100px'),
            'ban_date'     => array('text' => '申请有效期', 'width' => '100px'),
        	'trade_gold'   => array('text' => '元宝数', 'width' => '80px'),
        	'item'         => array('text' => '道具', 'width' => '200px'),
        	'create_user'       => array('text' => '申请人', 'width' => '80px'),
			'create_time'       => array('text' => '申请时间', 'width' => '100px'),
			'status'            => array('text' => '状态', 'width' => '80px'),
			'action'            => array('text' => '操作', 'width' => '100px')
        );

        return $header;
    }
    
    public static function insideHeader(){
    	$header = array(
    			'id'           => array('text' => 'ID', 'width' => '90px'),
    			'special_id'   => array('text' => '专服', 'width' => '160px'),
    			'server_id'    => array('text' => '服务器', 'width' => '100px'),
    			'channel_id'   => array('text' => '渠道', 'width' => '100px'),
    			'role_name'    => array('text' => '角色名', 'width' => '160px'),
    			'role_id'      => array('text' => '角色ID', 'width' => '160px'),
    			'account'      => array('text' => '账号', 'width' => '160px'),
    			'create_user'       => array('text' => '申请人', 'width' => '80px'),
    			'create_time'       => array('text' => '申请时间', 'width' => '100px'),
    			
    			'level'             => array('text' => '等级', 'width' => '70px'),
    			'stop_day'             => array('text' => '停登天数', 'width' => '100px'),
    			'history_num'             => array('text' => '历史申请总额', 'width' => '150px'),
    			'is_stop'             => array('text' => '是否停封', 'width' => '100px'),
    			'active'             => array('text' => '活跃查询', 'width' => '100px'),
    			'is_trade'             => array('text' => '是否开启价交易', 'width' => '120px'),
    			'limit_everyday'             => array('text' => '每天交易限额', 'width' => '110px'),
    			
    			'action'            => array('text' => '操作', 'width' => '100px')
    	);
    	
    	return $header;
    }

    public static function indexData($params){
    	$user = AccAuth::me();
    	if($user['IsAdmin'] != 1){
    		$params['create_user'] = $user['UserID'];
    	}
    	
    	$specialList = array();

        $modInsidePlayer = new ModInsidePlayer();
        $result = $modInsidePlayer->page($params);
        
        $modSpecialServicer = new ModSpecialServicer();
        $spList = $modSpecialServicer->getSpecialServer();
        $specialList = array();
        foreach ($spList as $key=>$val){
        	$specialList[$val['special_id']]['name'] = $val['name'];
        }

        $serverList = PubServer::getOptions($platform, $game, 'id');
        $statusList = self::status();
        $userList = self::userList();
        $length = count($result['data']);
        foreach($result['data'] as &$v){
        	$type = 1;
        	if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
        		$type = 2;
        	}
        	
        	$v['special_id'] = $specialList[$v['special_id']]['name'];
        	$v['server_id'] = $serverList[$v['server_id']];
        	
        	if($v['ban_date'] == 2000000000){
        		$v['ban_date'] = '永久';
        	}else{
        		$v['ban_date'] = date('Y-m-d', $v['ban_date']);
        	}
        	
        	if($v['is_qx'] == 1){
        		$v['is_qx'] = '是';
        	}else{
        		$v['is_qx'] = '否';
        	}
        	
        	$item_array = json_decode($v['item'],true);
        	$item = '';
        	foreach ($item_array as $key=>$val){
        		if($val['is_bind'] == 1){
        			$bind = '绑定';
        		}
        		if($val['is_bind'] == 0){
        			$bind = '不绑定';
        		}
        		$item .= $val['item_name'].'-'.$val['item_id'].'-'.$val['item_number'].'-'.$bind;
        		$item .= '</br>';
        	}
        	$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        	$v['item'] = $item;

        	$v['create_user'] = $userList[$v['create_user']];
        	$status = $v['status'];
        	if($status == 1){
        		$v['status'] = $statusList[$v['status']];
        	}
        	if($status == 2){
        		$v['status'] = '<font color="blue">'.$statusList[$v['status']].'</font>';
        		$v['action'] .= '<a href="javascript:;" name="xinfo" data-id="'.$v['id'].'"><font color="blue">审批信息</font></a>';
        	}
        	if($status == 3){
        		$v['status'] = '<font color="red">'.$statusList[$v['status']].'</font>';
        		$v['action'] .= '<a href="javascript:;" name="xinfo" data-id="'.$v['id'].'"><font color="blue">审批信息</font></a>';
        	}
        	
        	$v['action'] .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=insidePlayer/delete',$type).'&id='.$v['id'].'" confirm="是否要删除该条信息?">删除</a>';
        }
        unset($v);

        return $result;
    }
    
    public static function approveData($params){
    	$user = AccAuth::me(); 
    	$specialList = array();
    
    	$modInsidePlayer = new ModInsidePlayer();
    	$result = $modInsidePlayer->page($params);
    
    	$modSpecialServicer = new ModSpecialServicer();
    	$spList = $modSpecialServicer->getSpecialServer();
    	$specialList = array();
    	foreach ($spList as $key=>$val){
    		$specialList[$val['special_id']]['name'] = $val['name'];
    	}
    
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	$statusList = self::status();
    	$userList = self::userList();
    	$length = count($result['data']);
    	foreach($result['data'] as &$v){
    		$type = 1;
    		if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
    			$type = 2;
    		}
    		 
    		$v['special_id'] = $specialList[$v['special_id']]['name'];
    		$v['server_id'] = $serverList[$v['server_id']];
    		 
    		if($v['ban_date'] == 2000000000){
    			$v['ban_date'] = '永久';
    		}else{
    			$v['ban_date'] = date('Y-m-d', $v['ban_date']);
    		}
    		 
    		if($v['is_qx'] == 1){
    			$v['is_qx'] = '是';
    		}else{
    			$v['is_qx'] = '否';
    		}
    		 
    		$item_array = json_decode($v['item'],true);
    		$item = '';
    		foreach ($item_array as $key=>$val){
    			if($val['is_bind'] == 1){
    				$bind = '绑定';
    			}
    			if($val['is_bind'] == 0){
    				$bind = '不绑定';
    			}
    			$item .= $val['item_name'].'-'.$val['item_id'].'-'.$val['item_number'].'-'.$bind;
    			$item .= '</br>';
    		}
    		$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
    		$v['item'] = $item;
    
    		$v['create_user'] = $userList[$v['create_user']];
    	    $status = $v['status'];
			if($status == 1){
				$v['status'] = '<span class="xstatus">'.$statusList[$v['status']].'</span>';
				$v['action'] .= '<div class="xaction">';
				$v['action'] .= '<a href="javascript:;" name="approve" data-id="'.$v['id'].'"><font color="blue">批准</font></a>';
				$v['action'] .= '&nbsp;&nbsp;&nbsp;<a href="javascript:;" name="reject" data-id="'.$v['id'].'"><font color="red">驳回</font></a>';
				$v['action'] .= '</div>';
			}
			if($status == 2){
				$v['status'] = '<font color="blue">'.$statusList[$v['status']].'</font>';
				$v['action'] .= '<a href="javascript:;" name="xinfo" data-id="'.$v['id'].'"><font color="blue">审批信息</font></a>';
			}
			if($status == 3){
				$v['status'] = '<font color="red">'.$statusList[$v['status']].'</font>';
				$v['action'] .= '<a href="javascript:;" name="xinfo" data-id="'.$v['id'].'"><font color="blue">审批信息</font></a>';
			}
    		 
    		
    	}
    	unset($v);
    
    	return $result;
    }
    
    
    public static function insideData($params,$export=false){
    	$user = AccAuth::me();
    	$specialList = array();
    
    	$modInsidePlayer = new ModInsidePlayer();
    	$params['status'] = 2;    	
    	
    	if($export == true){
    		$result = $modInsidePlayer->select($params);
    		$result = self::handleData($result,true);
    	}else{
    		$result = $modInsidePlayer->page($params);
    		$result['data'] = self::handleData($result['data'],false);
    	}
    	
    	return $result;
    }
    
    public static function handleData($handleData,$export=false){
    	$modSpecialServicer = new ModSpecialServicer();
    	$spList = $modSpecialServicer->getSpecialServer();
    	$specialList = array();
    	foreach ($spList as $key=>$val){
    		$specialList[$val['special_id']]['name'] = $val['name'];
    	}
    	 
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	$statusList = self::status();
    	$userList = self::userList();
    	foreach($handleData as &$v){
    	
    		$server = $v['server_id'];
    		$v['special_id'] = $specialList[$v['special_id']]['name'];
    		$v['server_id'] = $serverList[$v['server_id']];
    		$v['create_user'] = $userList[$v['create_user']];
    		$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
    		 
    		$params = array(
    				'uid'    => $v['role_name'],
    				'search_type'    => 1,
    		);
    		$api = new ApiService($server);
    		$roleInfo = $api->roleInfo($params);
    		if($roleInfo['state']){
    			$data_array = json_decode($roleInfo['data'],true);
    			$data = $data_array[0];
    				
    			$v['level'] = $data['level'];
    			$last_logout_time = $data['last_logout_time'];
    	
    			$stop_day=round((time()-$last_logout_time)/3600/24);
    			$v['stop_day'] = $stop_day;
    		}else{
    			$v['role_name'] = $v['role_name'].'<font color="red">（该角色不存在）</font>';
    		}
    		
    		if($export == false){
    			$v['action'] .= '<div class="xaction">';
    			$v['action'] .= '<a href="javascript:;" name="seal_off" data-id="'.$v['id'].'"><font color="blue">封禁</font></a>';
    			$v['action'] .= '&nbsp;&nbsp;&nbsp;<a href="javascript:;" name="undo" data-id="'.$v['id'].'"><font color="red">解封</font></a>';
    			$v['action'] .= '</div>';
    		}
    		
    		if(!$roleInfo['state']){
    			unset($v['action']);
    		}
    	
    	}
    	unset($v);
    	return $handleData;
    }
    
    


    public static function doAdd($params){
        $user = AccAuth::me();

        $modInsidePlayer = new ModInsidePlayer();
        
        $person_item = array();
        foreach ($params['item_id'] as $key=>$val){
        	$data = array();
        	$data['item_name'] = $params['item_name'][$key];
        	$data['item_id'] = $params['item_id'][$key];
        	$data['item_number'] = $params['item_number'][$key];
        	$data['is_bind'] = $params['is_bind'][$key];
        	array_push($person_item, $data);
        }
        $item = json_encode($person_item);

        $modInsidePlayer->platform      = 'test';
        $modInsidePlayer->game          = $params['g'];
        $modInsidePlayer->special_id    = $params['special_id'];
        $modInsidePlayer->server_id     = $params['server_id'];
        $modInsidePlayer->role_name     = $params['role_name'];
        $modInsidePlayer->role_id       = $params['role_id'];
        $modInsidePlayer->account       = $params['account'];
        $modInsidePlayer->real_name     = $params['real_name'];
        $modInsidePlayer->position      = $params['position'];
        $modInsidePlayer->is_qx         = $params['is_qx'];
        $params['ban_date'] = (isset($params['ban_forever']) || empty($params['ban_date'])) ? 2000000000 : strtotime($params['ban_date']);
        $modInsidePlayer->ban_date      = $params['ban_date'];
        $modInsidePlayer->trade_gold    = $params['trade_gold'];
        $modInsidePlayer->item          = $item;
        $modInsidePlayer->create_user   = $user['UserID'];
        $modInsidePlayer->create_time   = time();
        $modInsidePlayer->update_user   = $user['UserID'];
        $modInsidePlayer->update_time   = time();

        $result = $modInsidePlayer->save();
        return $result;
    }

    public static function get($id){
        $modInsidePlayer = new ModInsidePlayer();
        $obj = $modInsidePlayer->findByPk($id);

        if($obj){
            $info = $obj->getAttributes();
            return $info;
        }else{
            return false;
        }
    }

    /**
     * 删除
     * @param unknown $id
     * @return boolean
     */
    public static function delete($id){
        if(empty($id)) return false;

        $modInsidePlayer = new ModInsidePlayer();
        $modInsidePlayer->deleteByPk($id);
        return true;
    }
    

    public static function status(){
    	return array(
    			'1' => '未批准',
    			'2' => '已批准',
    			'3' => '已驳回',
    	);
    }
    
    public static function getApproval($id){
    	$modInsidePlayer = new ModInsidePlayer();
    	$obj = $modInsidePlayer->findByPk($id);
    
    	if($obj){
    		$result = array();
    		$info = $obj->getAttributes();
    		if($info['approval_info'] == ''){
    			return $result;
    		}
    		$approval_info_array = explode(';', $info['approval_info']);
    		$userList = self::userList();
    		foreach ($approval_info_array as $key=>$val){
    			$listOne = json_decode($val,true);
    			$listOne['user'] = $userList[$listOne['user']];
    			$result[] = $listOne;
    		}
    		return $result;
    	}else{
    		return false;
    	}
    }
    
    /**
     * 批准、驳回
     * @param unknown $id
     * @param unknown $status
     * @return multitype:number string
     */
    public static function setStatus($id,$status){
    	if(empty($id) || empty($status)){
    		$result = array('status'=>0,'msg'=>'参数不完整','data'=>'');
    		return $result;
    	}
    	 
    	$modInsidePlayer = new ModInsidePlayer();
    	$user = AccAuth::me();
    	$obj = $modInsidePlayer->findByPk($id);
    	 
    	if(!$obj){
    		$result = array('status'=>0,'msg'=>'参数错误','data'=>'');
    		return $result;
    
    	}
    	$info = $obj->getAttributes();
    	 
    	 
    	//批准时先发送请求接口，请求成功则申请成功，请求失败则驳回
    	$new_status = 2;
    	 
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	if($status == 2){
    		//$sresult = self::sendVer2($info);
    		$sresult['state'] = true;
    		if($sresult['state']){
    			$new_status = 2;
    			$approval_info .= '批准<br/>发送服务器 <font color="red">'.$serverList[$info['server_id']].' </font>成功<br/>';
    		}else{  //发送请求失败
    			$new_status = 3;
    			$approval_info = '批准<br/>发送服务器 <font color="red">'.$serverList[$info['server_id']].' </font>失败，失败原因 <font color="red">'.$sresult['msg'].'</font>,该申请自动驳回<br/>';
    		}
    	}else if($status == 3){
    		$new_status = 3;
    		$approval_info = '驳回';
    	}
    
    	$approval = array();
    	$approval['time'] = date('Y-m-d H:i',time());
    	$approval['user'] = $user['UserID'];
    	$approval['info'] = $approval_info;
    	 
    	$update = array();
    	$update['status'] = $new_status;
    	$update['approval_user'] = trim($info['approval_user'].','.$user['UserID'],',');
    	$update['approval_info'] = trim($info['approval_info'].';'.json_encode($approval),';');  //审批信息
    	$mresult = $modInsidePlayer->updateByPk($id, $update);
    
    	if($mresult){
    		$result = array('status'=>1,'msg'=>'操作成功，'.$approval_info,'data'=>$new_status);
    		return $result;
    	}else{
    		$result = array('status'=>0,'msg'=>'操作失败，'.$approval_info,'data'=>'');
    		return $result;
    	}
    	 
    }


    public static function banUser($id,$actiontype){
    	if(!$id || $actiontype == ''){
    		$result = array('status'=>0,'msg'=>'参数错误','data'=>'');
    		return $result;
    	}
    	
    	$modInsidePlayer = new ModInsidePlayer();
    	$obj = $modInsidePlayer->findByPk($id);
    	if($obj){
    		$info = $obj->getAttributes();
    		$server_id = $info['server_id'];
    		$list = $info['role_name'];
    		
    		$params = array(
    				'bantype'       => 2,  //按角色名
    				'list'          => $list,
    				'actiontype'    => $actiontype,
    				'bandate'       => 2000000000,
    				'reason'        => '内玩玩家',
    		);
    		$params['bandate'] =  2000000000;
    		$params['list'] = CommonService::getStringFromMultiLine($params['list']);

    		$api = new ApiService($server_id);
    		$banResult = $api->ban($params);
    		if($banResult['state']){
    			$result = array('status' => 1, 'msg' =>'操作成功','data'=>'');
    		}else{
    			$result = array('status' => 0, 'msg' => $result['msg'],'data'=>'');
    		}
    		
    	}else{
    		$result = array('status'=>0,'msg'=>'参数错误','data'=>'');
    	}
    	return $result;
    }




    
    
    
}